package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.Customer;
import io.github.talelin.latticy.service.CustomerService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * 用户信息，这里指的是C端的用户
 *
 * @author YiHua
 * @date 2021/11/16 1:10 上午
 */
@RestController
@RequestMapping("/v1/user")
public class CustomerController {

     @Autowired
     private CustomerService customerService;        // C端客户业务层

     /**
      * 查询多个用户,支持分页
      *
      * @param pageNum  当前页 false
      * @param pageSize 每页条数 false
      */
     @GetMapping("/page")
     public PageResponseVO<Customer> page(
         @RequestParam(name = "pageSize", required = false, defaultValue = "10")
         @Min(value = 1, message = "{page.pageSize.min}")
         @Max(value = 30, message = "{page.pageSize.max}") Long pageSize,
         @RequestParam(name = "pageNum", required = false, defaultValue = "0")
         @Min(value = 0, message = "{page.pageNum.min}") Long pageNum
     ) {
          IPage<Customer> paging = this.customerService.paging(pageNum, pageSize);
          return PageUtil.build(paging);
     }

     /**
      * 查询单个用户信息
      *
      * @param id userId
      */
     @GetMapping("/{id}")
     public Customer get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long id) {
          return customerService.getById(id);
     }

     /**
      * 按名称搜索用户
      *
      * @param pageSize 每页条数
      * @param pageNum  当前页
      * @param keyword  名称关键词
      * @return
      */
     @GetMapping("/search")
     public PageResponseVO<Customer> page(
         @RequestParam(name = "pageSize", required = false, defaultValue = "10")
         @Min(value = 1, message = "{page.pageSize.min}")
         @Max(value = 30, message = "{page.pageSize.max}") Long pageSize,
         @RequestParam(name = "pageNum", required = false, defaultValue = "0")
         @Min(value = 0, message = "{page.pageNum.min}") Long pageNum,
         @RequestParam(name = "keyword") String keyword
     ) {
          IPage<Customer> paging = this.customerService.search(pageNum, pageSize, keyword);
          return PageUtil.build(paging);
     }
}
